//
//  HSPWebChangeViewController.m
//  Icar
//
//  Created by dllo on 15/10/24.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPWebChangeViewController.h"
#import "BaseHeader.h"
@interface HSPWebChangeViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIView *titleView;
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic,strong)MBProgressHUD *HUD;
@property (strong, nonatomic)FeThreeDotGlow *threeDot;

@end

@implementation HSPWebChangeViewController
- (void)viewWillAppear:(BOOL)animated
{
    [_webView reload];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 64)];
    [_titleView setBackgroundColor:BLUECOLOR];
    UIImage *image1 = [UIImage imageNamed:@"5.png"];
    UIImage *img1 = [self scaleToSize:image1 size:CGSizeMake(30 * ADJUSTWIDTH, 30 * ADJUSTHEIGHT)];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(10 * ADJUSTWIDTH, 26 * ADJUSTHEIGHT, 90 * ADJUSTWIDTH, 35 * ADJUSTHEIGHT)];
    [backButton setImage:img1 forState:UIControlStateNormal];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBack) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:backButton];
    [self.view addSubview:_titleView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    _webView.delegate =self;
    //设置webView不能上拉
    [(UIScrollView *)[[_webView subviews] objectAtIndex:0] setBounces:NO];
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/price/getPromotionv45/?modelId=%@&modelName=%@&serialId=%@&url=%@&dealerId%@&areaId=248&areaName=%@",self.modelChange.modelId,self.modelChange.modelName,[self.modelChange.serialGroupId description],self.modelChange.image,self.modelChange.dealerId,self.modelChange.area];
    NSString *str2 = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURLRequest *request9 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:str2]];
    [_webView  loadRequest:request9];
    [self.view addSubview:_webView];
}
- (void)backBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (UIImage *)scaleToSize : (UIImage *)img size:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
//加载完成的时候执行该方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if ([LYYNightModeHandle shareNightModeHandle].temp == YES) {
        //字体颜色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
        //页面背景色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
    }
    
}
//开始加载的时候执行该方法
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    _threeDot = [[FeThreeDotGlow alloc] initWithView:self.view blur:NO];
    [self.view addSubview:_threeDot];
    // Start
    [_threeDot showWhileExecutingBlock:^{
        [self myTask];
    } completion:^{
        _threeDot.hidden = YES;
    }];
    
    
}
- (void)myTask
{
    // Do something usefull in here instead of sleeping ...
    sleep(1.5);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
